package qlsl.androiddesign.view.subview.activityview;

import java.io.File;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import qlsl.androiddesign.activity.commonactivity.PhotoAddActivity;
import qlsl.androiddesign.activity.subactivity.PostingCreateActivity;
import qlsl.androiddesign.activity.subactivity.PublishTypeActivity;
import qlsl.androiddesign.constant.Constant;
import qlsl.androiddesign.constant.IntentCodeConstant;
import qlsl.androiddesign.http.xutils.HttpProtocol;
import qlsl.androiddesign.listener.OnUploadFileListener;
import qlsl.androiddesign.method.UserMethod;
import qlsl.androiddesign.util.runfeng.FFileUtils;
import qlsl.androiddesign.util.runfeng.ToActivityUtil;
import qlsl.androiddesign.view.baseview.FunctionView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.photoselector.model.PhotoModel;
import com.photoselector.ui.PhotoSelectorActivity;
import com.qlsl.androiddesign.kuaiwa.R;

/**
 * 帖子发布页<br/>
 * 需要传入的键：<br/>
 * 传入的值类型： <br/>
 * 传入的值含义：<br/>
 * 是否必传 ：
 */
public class PostingCreateView extends FunctionView<PostingCreateActivity> {

	/**
	 * 视频文件
	 */
	private File file;

	public PostingCreateView(PostingCreateActivity activity) {
		super(activity);
		setContentView(R.layout.activity_posting_create);
	}

	protected void initView(View view) {
		setTitle("发布");
	}

	protected void initData() {

	}

	protected void initListener() {

	}

	public <T> void showData(T... t) {

	}

	public <T> void showNoData(T... t) {

	}

	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.tv_select:
			startActivityForResult(PublishTypeActivity.class,
					IntentCodeConstant.REQUEST_CODE_PUBLISH_TYPE);
			break;
		case R.id.layout_add_photo:
			doClickAddPhotoView(view);
			break;
		case R.id.layout_add_video:
			try {
				startActivityForResult(Intent.createChooser(
						ToActivityUtil.getAllFileIntent(), "请选择一个要上传的文件"), 10);
			} catch (android.content.ActivityNotFoundException ex) {
			}
			break;
		case R.id.tv_check:
			doClickCheckView(view);
			break;
		case R.id.btn_publish:
			doClickPublishView();
			break;
		}
	}

	@SuppressWarnings("unchecked")
	private void doClickAddPhotoView(View view) {
		List<File> files = (List<File>) view.getTag();
		Intent intent = new Intent(activity, PhotoAddActivity.class);
		if (files != null) {
			intent.putExtra("files", (Serializable) files);
		}
		startActivityForResult(intent,
				IntentCodeConstant.REQUEST_CODE_PHOTO_ADD);
	}

	private void doClickCheckView(View view) {
		view.setActivated(!view.isActivated());
	}

	@SuppressWarnings("unchecked")
	private void doClickPublishView() {
		Intent intent = (Intent) findViewById(R.id.tv_select).getTag();
		String title = ((EditText) findViewById(R.id.et_title)).getText()
				.toString();
		String content = ((EditText) findViewById(R.id.et_content)).getText()
				.toString();
		List<File> files = (List<File>) findViewById(R.id.layout_add_photo)
				.getTag();
		if (intent == null) {
			showToast("请选择分类");
			return;
		}
		if (TextUtils.isEmpty(title)) {
			showToast("请输入标题");
			return;
		}
		if (TextUtils.isEmpty(content)) {
			showToast("请输入内容");
			return;
		}
		if ((files == null || files.size() == 0) && file == null) {
			showToast("请添加图片或视频");
			return;
		}
		if (file != null) {
			if (files == null) {
				files = new ArrayList<File>();
			}
			files.add(file);
		}
		String firstsection = intent.getStringExtra("firstsection");
		String cnfirstsection = intent.getStringExtra("cnfirstsection");
		String secondsection = intent.getStringExtra("secondsection");
		String cnsecondsection = intent.getStringExtra("cnsecondsection");
		boolean isTop = ((TextView) findViewById(R.id.tv_check)).isActivated();
		setProgressBarText("正在发布");
		showProgressBar();
		new HttpProtocol().setMethod("sendpostings")
				.addParam("postingsname", title)
				.addParam("postingscontent", content)
				.addParam("firstsection", firstsection)
				.addParam("cnfirstsection", cnfirstsection)
				.addParam("secondsection", secondsection)
				.addParam("cnsecondsection", cnsecondsection)
				.addParam("fuserid", UserMethod.getUser().getUserId())
				.addParam("istop", isTop).addParam("city", Constant.city)
				.removeUserParam().uploadFiles(files, null, listener);
	}

	@SuppressWarnings("unchecked")
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == IntentCodeConstant.REQUEST_CODE_PHOTO_PICKED) {
			if (resultCode == Activity.RESULT_OK) {
				Bundle bundle = data.getExtras();
				if (bundle == null) {
					showToast("无法加载图片");
					return;
				}
				List<PhotoModel> selectPhotos = (List<PhotoModel>) bundle
						.getSerializable(PhotoSelectorActivity.KEY_RESULT);
				if (selectPhotos.size() > 0) {
					String path = selectPhotos.get(0).getOriginalPath();
					cropPhoto(path);
				}
			}
		} else if (requestCode == IntentCodeConstant.CROP_PHOTO) {
			if (data == null) {
				return;
			}
			Bitmap bitmap = data.getExtras().getParcelable("data");
			File file = saveBitmapToFile(bitmap);
		} else if (requestCode == IntentCodeConstant.REQUEST_CODE_PUBLISH_TYPE) {
			if (data == null) {
				return;
			}
			String firstsection = data.getStringExtra("firstsection");
			String secondsection = data.getStringExtra("secondsection");
			TextView tv_select = ((TextView) findViewById(R.id.tv_select));
			tv_select.setText(firstsection + ">" + secondsection);
			tv_select.setTag(data);
		} else if (requestCode == IntentCodeConstant.REQUEST_CODE_PHOTO_ADD) {
			if (data == null) {
				return;
			}
			List<File> files = (List<File>) data.getSerializableExtra("files");
			View addPhotoView = findViewById(R.id.layout_add_photo);
			addPhotoView.setTag(files);
			TextView tv_photo_num = (TextView) addPhotoView
					.findViewById(R.id.tv_photo_num);
			tv_photo_num.setText("(" + files.size() + "/9)");
		} else if (requestCode == 10) {// 10
			if (resultCode == Activity.RESULT_OK) {
				View layout_add_video = findViewById(R.id.layout_add_video);
				Uri uri = data.getData();
				String url = FFileUtils.getPath(activity, uri);
				file = new File(url);
				TextView tv_video_num = (TextView) layout_add_video
						.findViewById(R.id.tv_video_num);
				tv_video_num.setText("(1/1)");
			}

		}
	};

	private OnUploadFileListener listener = new OnUploadFileListener() {

		public void onSuccess(JSONObject jo) {
			showToast("发布成功");
			setRefresh(true);
			activity.finish();
		}
	};
}
